package com.lin.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lin.mapper.Plus.UserMapperPlus;
import com.lin.mapper.UserMapper;
import com.lin.pojo.user;
import com.lin.response.CommonResp;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserService extends ServiceImpl<UserMapperPlus, user> {
    @Autowired
    private UserMapper userMapper;

    public CommonResp Login(String name, String password){
        Integer count = userMapper.SelectCount(name,password);
        CommonResp<user> commonResp = new CommonResp<>();
        if(count>0){
            user user = userMapper.Select_Login(name,password);
            commonResp.setSuccess(true);
            commonResp.setMessage("找到用户。");
            commonResp.setContent(user);
        }
        else {
            commonResp.setSuccess(false);
            commonResp.setMessage("不存在该用户。");
            commonResp.setContent(null);
        }
        System.out.println(commonResp.getContent());
        return commonResp;
    }

    public CommonResp Register(String name,String password,String username,String studentNo){
        Integer count = userMapper.SelectCount_ByName(name);
        CommonResp<user> commonResp = new CommonResp<>();
        if(count>0){
            commonResp.setSuccess(false);
            commonResp.setMessage("该用户已存在。");
            commonResp.setContent(null);
        }
        else {
            user user = new user();
            user.setName(name);
            user.setPassword(password);
            user.setUsername(username);
            user.setStudentNo(studentNo);
            save(user);
            commonResp.setSuccess(true);
            commonResp.setMessage("该用户成功注册。");
            commonResp.setContent(null);
        }
        return commonResp;
    }

    public CommonResp person(String username){
        user user = userMapper.SelectPerson(username);
        CommonResp<user> commonResp = new CommonResp<>();
        if(user != null){
            commonResp.setSuccess(true);
            commonResp.setMessage("查找成功.");
            commonResp.setContent(user);
        }
        else {
            commonResp.setSuccess(false);
            commonResp.setMessage("查找失败.");
            commonResp.setContent(null);
        }
        return commonResp;
    }

    public CommonResp recharge(String name,Integer money){
        user user = userMapper.SelectPerson(name);
        Integer cur_money = user.getMoney() + money;
        Integer flag = userMapper.UpdateMoney(name,cur_money);
        CommonResp<user> commonResp = new CommonResp<>();
        if(flag > 0){
            commonResp.setSuccess(true);
            commonResp.setMessage("充值成功.");
            commonResp.setContent(user);
        }
        else {
            commonResp.setSuccess(false);
            commonResp.setMessage("充值失败.");
            commonResp.setContent(null);
        }
        return commonResp;
    }
}
